package org.base.common.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.base.common.annotation.Log;
import org.base.common.service.IService;
import org.base.sys.enums.BizType;
import org.base.sys.enums.OpType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 基础控制器类
 **/
@Validated
public class BaseController<T, S extends IService> {


    @Autowired
    protected S service;

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("/remove/{id}")
    @ApiImplicitParam(dataType = "String", paramType = "header", name = "token", value = "token", required = true)
    @Transactional
    @Log(bizType = BizType.DELETE, opType = OpType.MANAGE)
    public boolean removeById(@NotNull @PathVariable("id") Long id) {
        service.removeById(id);
        return true;
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/batchRemove")
    @ApiOperation(value = "根据ID批量删除")
    @Transactional
    @Log(bizType = BizType.DELETE, opType = OpType.MANAGE)
    public boolean batchRemove(@RequestParam(value = "ids") Long[] ids) {
        return service.removeByIds(Arrays.asList(ids));
    }


}
